//
//  YWWriteNoteCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/9.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWWriteNoteCell.h"
#import "Masonry.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "YWEquipment.h"

@interface YWWriteNoteCell()
/*  填写备注的textView */
@property (nonatomic, strong) UITextView * commentTextView;
/*  textView的placeHolder */
@property (nonatomic, strong) UILabel * placeHolderLabel;
@end

@implementation YWWriteNoteCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpSubviews];
    }
    
    return self;
}

- (void)setEquipment:(YWEquipment *)equipment{
    _equipment = equipment;
    self.commentTextView.text = equipment.psText.length>0?equipment.psText:nil;
    self.placeHolderLabel.hidden = equipment.psText.length>0 ? YES : NO;
}

- (void)setTextViewPlaceHolder:(NSString *)textViewPlaceHolder{
    self.placeHolderLabel.text = textViewPlaceHolder;
}

- (void)setTextViewContent:(NSString *)textViewContent{
    _textViewContent = textViewContent;
    self.commentTextView.text = textViewContent;
    self.placeHolderLabel.hidden = textViewContent.length>0 ? YES : NO;
}

- (void)setInputTextMaxCount:(NSInteger)inputTextMaxCount{
    _inputTextMaxCount = inputTextMaxCount;
}

- (void)setUpSubviews{
    [self.commentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    [self.placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.mas_equalTo(self.commentTextView).offset(7);
    }];
    
}
- (UILabel*)placeHolderLabel
{
    if (!_placeHolderLabel) {
        UILabel* placeHolderLabel = [[UILabel alloc] init];
        placeHolderLabel.font = kContentFont;
        placeHolderLabel.textColor = [UIColor customTextGrayColor];
        placeHolderLabel.text = @"填写备注";
        [self.commentTextView addSubview:placeHolderLabel];
        _placeHolderLabel = placeHolderLabel;
    }
    return _placeHolderLabel;
}
- (UITextView *)commentTextView{
    if (!_commentTextView) {
        UITextView * textView = [[UITextView alloc] init];
        textView.font = kContentFont;
        textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        textView.layer.borderWidth = 1.0;
        textView.clipsToBounds = YES;
        textView.layer.cornerRadius = 4.0;
        textView.delegate = self;
        [self.contentView addSubview:textView];
        _commentTextView = textView;
    }
    return _commentTextView;
}
- (void)textViewDidChange:(UITextView*)textView
{
    if (textView.text.length == 0) {
        self.placeHolderLabel.hidden = NO;
    }
    else {
        self.placeHolderLabel.hidden = YES;
    }
    NSString *toBeString = textView.text;
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    NSInteger KNoteTextMax = _inputTextMaxCount >0 ? _inputTextMaxCount : 100;
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > KNoteTextMax)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:KNoteTextMax];
            if (rangeIndex.length == 1)
            {
                textView.text = [toBeString substringToIndex:KNoteTextMax];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, KNoteTextMax)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
        _equipment.psText = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSString *note = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([self.delegate respondsToSelector:@selector(passNote:)]) {
            [self.delegate passNote:note];
        }
    }
}

@end
